<?php

class Mservice extends CI_Model {
	
	public $table = 'service';
	public $pri_key="service_id";


	function __construct()

	{

		parent::__construct();

	}
	// ********** search ***********//
	 function search()

	{

		$keyword = htmlentities($this->input->post('keyword'), ENT_QUOTES, 'UTF-8');

    $type = $this->input->post('search_type');

		$sql = "SELECT *

            FROM user WHERE ";

    if ($type == 'id')

      $sql .= "user_id = '$keyword' ";

    elseif ($type == 'name')

      $sql .="first_name LIKE '%$keyword%' or last_name   LIKE '%$keyword%' ";
	else
		$sql .= "first_name LIKE '%$keyword%' or last_name   LIKE '%$keyword%' ";


    $sql .= "ORDER BY user_id DESC";



		$Q = $this->db->query($sql);

		if ($Q->num_rows() > 0)

		{

			return $Q->result_array();

		}



		return false;

	}
public function check_username($username,$id='')
	{
		$sql="select * from user where user_name = '".$username."'";
		//$this->db->where('user_email =',$email);
		if($id != '')
		{
			//$this->db->where("user_id !=",$id);
			$sql= $sql." and user_id != ".$id;
			
		}
		$q= $this->db->query($sql);
	
		if($q->num_rows() >0)
			return false; //co the su dung email nay 
		return true; // email nay da ton tai va ko su dung duoc
			
 	}
	public function insert($data)
	{

		if($this->db->insert($this->table,$data))
			return true;
		
		return false;

	}
	public function update($data,$id)
	{
		
		$where = $this->pri_key." =".$id;
		if($this->db->update($this->table,$data,$where))
			return true;
		
		return false;
	
	}
	
	
	
	public function info_form_id($id)
	{
		if(is_numeric($id) && $id !="")
		{
		$this->db->where($this->pri_key.' =',$id);
		return $this->db->get($this->table)->result_array();
		}
		else
			return false;
	}
	
		public function get_data()
	{
		
		if($this->db->count_all($this->table) >0)
			//return $this->db->get($this->table)->result_array();
			{
				return $this->db->get($this->table)->result_array();
			}
		return false;
	}
	
	

	public function list_data($num,$off)
	{
		$this->db->limit($off,$num);
		$this->db->order_by($this->pri_key." DESC");
		if($this->db->count_all($this->table) >0)
			//return $this->db->get($this->table)->result_array();
			{
				return $this->db->get($this->table)->result_array();
			}
		return false;
	}
	// dem so record 
	function count_all()
	{
		return $this->db->count_all_results($this->table);
	}
	
	
	
	

  function resource_search()

	{

		$keyword = htmlentities($this->input->post('keyword'), ENT_QUOTES, 'UTF-8');

    $type = $this->input->post('search_type');

		$sql = "SELECT id, avatar, date_join,

                   last_name, first_name, full_name, phone, address,

                   active, email, last_login

            FROM human_resources WHERE ";

    if ($type == 'id')

      $sql .= "id = '$keyword' ";

    elseif ($type == 'first_name')

      $sql .= "first_name LIKE '%$keyword%' ";

    elseif ($type == 'full_name')

      $sql .= "full_name LIKE '%$keyword%' ";



    $sql .= "ORDER BY id DESC";



		$Q = $this->db->query($sql);

		if ($Q->num_rows() > 0)

		{

			return $Q->result_array();

		}



		return false;

	}



	





	function delete()

	{

		if (isset($_POST['select_id']))

		{

			foreach($_POST['select_id'] as $id) {

			//	$row = $this->info_from_id($id);

		

				//xoa file

		//		if ($row['avatar'] != 'null') unlink($row['avatar']);

		

				//xoa CSDL

			//	$sql = "DELETE FROM human_resources WHERE id=$id";
				$this->db->where($this->pri_key, $id);
				$this->db->delete($this->table); 
				//$this->db->query($sql);

			}			

		}

	}



 






}//end model class





/* End of file mhumans.php */

/* Location: ./system/application/models/mhumans.php */

?>